package com.suoluo.zenadmin.service.crawler.resolver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import com.suoluo.zenadmin.service.crawler.bean.HotelReviewRecordBean;



/**
 * tripadvisor的网页抓取器
 * 
 * @author xiongxu
 * 
 */
class TripadvisorCrawlerResolver extends AbstractCrawlerResolver {

	public TripadvisorCrawlerResolver(CrawlerResovlerTask task) {
		super(task);
	}

	@Override
	protected int resolveReviewCount(Document doc) {
		String reviewCountStr = doc.select("span[property=v:count]").text()
				.trim();
		reviewCountStr = reviewCountStr.replaceAll(",", "");
		int reviewCount = Integer.parseInt(reviewCountStr);
		return reviewCount;
	}

	@Override
	protected double resolveReviewScore(Document doc) {
		String str = doc.select("span.percent").text();
		str = str.substring(0, str.indexOf('%')).trim();
		double reviewScore = Double.parseDouble(str);
		return reviewScore;
	}

	@Override
	protected double resolveReviewFullScore(Document doc) {
		return 100;
	}

	@Override
	protected String resolveReviewLevel(Document doc) {
		return null;
	}

	@Override
	protected int resolveReviewRank(Document doc) {
		String rank = doc.select("b.rank_text").first().text();
		String reviewCountNum = rank.replaceAll("[^\\d]", "");
		// rank = rank.substring(rank.indexOf('#')+1).trim();
		return Integer.parseInt(reviewCountNum.trim());
	}

	@Override
	protected List<HotelReviewRecordBean> resolveReviewRecords(Document doc) {
		List<HotelReviewRecordBean> list = new ArrayList<HotelReviewRecordBean>();
		Elements eles = doc.select("div.reviewSelector");
		Iterator<Element> iter = eles.iterator();
		int temp = 0;

		while (iter.hasNext()) {
			if (temp == 5) {
				break;
			}
			Element ele = iter.next();

			// 如果标题和描述没有，则过滤掉该评论
			String title = ele.select("span.noQuotes").text();
			String desc = ele.select("p.partial_entry").text();
			if (!StringUtils.hasLength(title) || !StringUtils.hasLength(desc)) {
				continue;
			}
			HotelReviewRecordBean review = new HotelReviewRecordBean();
			review.setRecordTitle(title);
			review.setRecordDesc(desc);
			review.setRecordAutor(ele.select("span.greyMemberNames").text());
			review.setRecordTime(ele.select("span.relativeDate").text());
//			String str = ele.select("img.sprite-ratings").attr("content")
//					.trim();
//			double score = Double.parseDouble(str);
			review.setRecordScore(5);
			review.setRecordFullScore(5.0);
			list.add(review);
			temp++;
		}
		return list;
	}

//	public static void main(String[] args) {
//		CrawlerResovlerTask task = new CrawlerResovlerTask(null);
//		task.setReviewUrl("http://www.tripadvisor.com/Hotel_Review-g298342-d316743-Reviews-LUX_Belle_Mare-Belle_Mare.html");
//		TripadvisorCrawlerResolver r = new TripadvisorCrawlerResolver(task);
//		r.doResolver();
//	}
}
